package com.orhon.smartcampus.modules.systemctl.fetcher;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.systemctl.dao.ModulesDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class ModuleFetcher implements DataFetcher {

	@Autowired
	ModulesDao modulesDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		int id = Integer.parseInt(env.getArgument("id").toString());
		String hql = "from Modules where deleted_at = null and id = "+id;
		Modules item = modulesDao.findId(hql , null);
		if(item!=null) {

			List<GQLMenus> gqlMenus = null;
			if (env.getSelectionSet().contains("menus")){
				item.getMenus().size();
				List<Menus> menus = item.getMenus();
				gqlMenus = MenuMapper.MAPPER.toMenusList(menus);

			}
			List<GQLSchool> gqlSchool = null;
			if (env.getSelectionSet().contains("schools")){
				item.getSchools().size();
				List<Schools> schools = item.getSchools();
				gqlSchool = SchoolMapper.MAPPER.toSchoolList(schools);

			}
			GQLModule module = ModuleMapper.MAPPER.mapModule(item);
			module.setMenus(gqlMenus);
			module.setSchools(gqlSchool);
			return module;
		}
		return null;




	}



}
